package com.wzu.controller;

import com.wzu.entity.Policy;
import com.wzu.entity.Result;
import com.wzu.service.PolicyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/policy")
@CrossOrigin
public class PolicyController {
    @Autowired
    private PolicyService policyService;

    // 查询所有政策
    @GetMapping("/list")
    public Result list() {
        List<Policy> list = policyService.getAllPolicies();
        return new Result("查询成功", list, 200);
    }

    // 按年份查找政策
    @GetMapping("/getByYear")
    public Result getByYear(@RequestParam String year) {
        Policy policy = policyService.getPolicyByYear(year);
        return new Result("查询成功", policy, 200);
    }

    // 添加政策
    @PostMapping("/add")
    public Result add(@RequestBody Policy policy) {
        if (policyService.addPolicy(policy)) {
            return new Result("添加成功", null, 200);
        } else {
            return new Result("添加失败", null, 400);
        }
    }

    // 更新政策
    @PutMapping("/update")
    public Result update(@RequestBody Policy policy) {
        if (policyService.updatePolicy(policy)) {
            return new Result("更新成功", null, 200);
        } else {
            return new Result("更新失败", null, 400);
        }
    }

    // 删除政策
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        if (policyService.deletePolicy(id)) {
            return new Result("删除成功", null, 200);
        } else {
            return new Result("删除失败", null, 400);
        }
    }
} 